﻿<cfscript>
	/* LIB 库*/
	public any function havePermission(any checkUrl){
		var result=false;
		for(var i =1; i<= arraylen(session.auth.allRsAuths);i++){
			if(!isNull(session.auth.allRsAuths[i].checkUrl) && session.auth.allRsAuths[i].resourcetypeid == 2 && trim(session.auth.allRsAuths[i].checkUrl)  == trim(arguments.checkUrl)){
				result = true;
			}
		}
		return result;
	}


	function ListRemoveByString(list, str) {
		var i = listLen(list);
		var mode = true;
		var delim = ",";

		if(arrayLen(arguments) gte 3) mode = arguments[3];
		if(arrayLen(arguments) gte 4) delim = arguments[4];

		for (i=ListLen(list, delim); i gte 1 ; i=i-1) {
			if(  (mode and findNoCase(str,listGetAt(list,i,delim))) or (not mode and not findNoCase(str,listGetAt(list,i,delim)))) list = listDeleteAt(list,i,delim);
		}
		return list;
	}

	function ListDeleteDuplicatesNoCase(list) {
	  var i = 1;
	  var delimiter = ',';
	  var returnValue = '';
	  if(ArrayLen(arguments) GTE 2)
	    delimiter = arguments[2];
	  list = ListToArray(list, delimiter);
	  for(i = 1; i LTE ArrayLen(list); i = i + 1)
	    if(NOT ListFindNoCase(returnValue, list[i], delimiter))
	      returnValue = ListAppend(returnValue, list[i], delimiter);
	  return returnValue;
	}

	function listdelete(variable)
	{
		var string = "";
		var ii = 1;
		var thisVar = "";
		var thisIndex = "";
		var array = "";
		var qs = "";
		if(arrayLen(arguments) GT 1)
			qs = arguments[2];
		array = listToArray(qs, ",");
		for(ii = 1; ii lte arrayLen(array); ii = ii + 1)
		{
			thisIndex = array[ii];
			thisVar = thisIndex;
			if(not listFindnocase(variable, thisVar))
				string = listAppend(string, thisIndex, ",");
		}
		return string;
	}

	function ListInCommon(List1, List2)
	{
		var TempList = "";
		var Delim1 = ",";
		var Delim2 = ",";
		var Delim3 = ",";
		var i = 0;
		switch(ArrayLen(arguments))
		{
			case 3:
			{
				Delim1 = Arguments[3];
				break;
			}
			case 4:
			{
				Delim1 = Arguments[3];
				Delim2 = Arguments[4];
				break;
			}
			case 5:
			{
				Delim1 = Arguments[3];
				Delim2 = Arguments[4];
				Delim3 = Arguments[5];
				break;
			}
		}

		for(i = 1; i LTE ListLen(List2, "#Delim2#"); i = i + 1)
		{
			if(ListFindNoCase(List1, ListGetAt(List2, i, "#Delim2#"), "#Delim1#"))
			{
				TempList = ListAppend(TempList, ListGetAt(List2, i, "#Delim2#"), "#Delim3#");
			}
		}
		Return TempList;
	}

	function listCompare(list1, list2)
	{
		var TempList = "";
		var Delim1 = ",";
		var Delim2 = ",";
		var Delim3 = ",";
		var i = 0;

		switch(ArrayLen(arguments))
		{
			case 3:
			{
				Delim1 = Arguments[3];
				break;
			}
			case 4:
			{
				Delim1 = Arguments[3];
				Delim2 = Arguments[4];
				break;
			}
			case 5:
			{
				Delim1 = Arguments[3];
				Delim2 = Arguments[4];
				Delim3 = Arguments[5];
				break;
			}
		}

		var list1Array = ListToArray(arguments.List1, Delim1);
		var list2Array = ListToArray(arguments.List2, Delim2);
		list1Array.removeAll(list2Array);
		return ArrayToList(list1Array, Delim3);
	}

	//获取CFC的Function，并且利用setXXX来进行动态设置值
	function getCFCFunctions(any rc, any cfc)
	{
		var props = getMetaData(cfc).properties;
		var arrKey = [];
		for(i = 1; i <= arraylen(props); i++)
		{
			for(key in rc)
			{
				if(lcase(props[i].name) == lcase(key))
				{
					ArrayAppend(arrKey, "set" & ucfirst(key) & "('" & rc[key] & "')");
				}
			}
		}
		return arrKey;
	}

	function deleEntityProperties(cfc,pro){
		var props = getMetaData(cfc).properties;
		for(var i = 1; i <= arraylen(props); i++)
		{
			for(var j = 1; j<=listlen(pro);j++){
				if(lcase(props[i].name) == lcase(listgetat(pro,j))){
					arraydeleteat(props,i);
				}
			}
		}
		return props;
	}

	function QueryStringChangeVar(variable,value){
		//var to hold the final string
		var string = "";
		//vars for use in the loop, so we don't have to evaluate lists and arrays more than once
		var ii = 1;
		var thisVar = "";
		var thisIndex = "";
		var array = "";
		var changedIt = 0;
		//if there is a third argument, use that as the query string, otherwise default to cgi.query_string
		var qs = cgi.query_string;
		if(arrayLen(arguments) GT 2)
			qs = arguments[3];

		//put the query string into an array for easier looping
		array = listToArray(qs,"&");
		//now, loop over the array and rebuild the string
		for(ii = 1; ii lte arrayLen(array); ii = ii + 1){
			thisIndex = array[ii];
			thisVar = listFirst(thisIndex,"=");
			//if this is the var, edit it to the value, otherwise, just append
			if(thisVar is variable){
				string = listAppend(string,thisVar & "=" & value,"&");
				changedIt = 1;
			}
			else{
				string = listAppend(string,thisIndex,"&");
			}
		}
		//if it was not changed, add it!
		if(NOT changedIt)
			string = listAppend(string,variable & "=" & value,"&");
		//return the string
		return string;
	}

	function queryStringDeleteVar(variable){
		//var to hold the final string
		var string = "";
		//vars for use in the loop, so we don't have to evaluate lists and arrays more than once
		var ii = 1;
		var thisVar = "";
		var thisIndex = "";
		var array = "";
		//if there is a second argument, use that as the query string, otherwise default to cgi.query_string
		var qs = cgi.query_string;
		if(arrayLen(arguments) GT 1)
			qs = arguments[2];
		//put the query string into an array for easier looping
		array = listToArray(qs,"&");
		//now, loop over the array and rebuild the string
		for(ii = 1; ii lte arrayLen(array); ii = ii + 1){
			thisIndex = array[ii];
			thisVar = listFirst(thisIndex,"=");
			//if this is the var, edit it to the value, otherwise, just append
			if(not listFind(variable,thisVar))
				string = listAppend(string,thisIndex,"&");
		}
		//return the string
		return string;
	}


	function getCurrentPageType(s){
		if(len(trim(arguments.s)) == 0){
			str = cgi.query_string;
		}else{
			str = s;
		}
		return listfirst(queryStringGetVar(str,"action"),".");
	}

	function queryStringGetVar(querystring, this_var_name){
		var re_found_struct = "";
		querystring = trim(querystring);

		re_found_struct = REFindNoCase("(^|[\?|&])#this_var_name#=([^\&]*)", querystring, 1, "true");
		// = re_found_struct.len & re_found_struct.pos

		if(arrayLen(re_found_struct.pos) gte 3) {
			if (re_found_struct.pos[3] GT 0) return urlDecode(mid(querystring, re_found_struct.pos[3], re_found_struct.len[3]));
			else return "";
		} else return "";
	}

	//递归获取ids列表值
	function RecursiveChild(any o)
	{
		if(structkeyexists(o, "getChildren") && arraylen(o.getChildren()) >= 0)
		{
			//特别注意变量i需要在循环的时候重新初始值，不然会错乱
			for(var i = 1; i <= arraylen(o.getChildren()); i++)
			{
				child = o.getChildren()[i];
				ids = listAppend(ids, child.getId());
				if(structkeyexists(child, "getChildren") && arraylen(child.getChildren()) >= 0)
				{
					//递归调用
					RecursiveChild(child);
				}
			}
		}
		return ids;
	}


	//递归改变添加、修改时间格式，原本有getCreatedFormat及getEditedFormat方法，现在已经删除，该函数已经作废
	//但可以重新写child的新方法，通过该递归方式去动态改变
	function changeDate(any o){
		if(structkeyexists(o, "getChildren") && arraylen(o.getChildren()) >= 0)
		{

			o.setCreated(o.getCreatedFormat());
			o.setEdited(o.getEditedFormat());

			//特别注意变量i需要在循环的时候重新初始值，不然会错乱
			for(var i = 1; i <= arraylen(o.getChildren()); i++)
			{
				child = o.getChildren()[i];

				child.setCreated(child.getCreatedFormat());
				child.setEdited(child.getEditedFormat());

				if(structkeyexists(child, "getChildren") && arraylen(child.getChildren()) >= 0)
				{
					//递归调用
					changeDate(child);
				}

			}
		}
	}

	function ListDeleteRight(list, numElements) {
		var i=0;
		var delimiter=",";

		if (Arraylen(arguments) gt 2) {
			delimiter=arguments[3];
		}

		if (numElements gt ListLen(list, delimiter)) return "";

		for (i=1; i lte numElements; i=i+1) {
			list=listDeleteAt(list, listLen(list, delimiter), delimiter);
		}
		return list;
	}


	function getPySpell(string s){
		if( arrayLen(arguments)>1 ){
			mode = arguments[1];
		}else{
			mode = 0;
		}

		var HanyuPinyinCaseType = CreateObject("java","net.sourceforge.pinyin4j.format.HanyuPinyinCaseType");
		var format = CreateObject("java","net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat");
		var HanyuPinyinToneType = CreateObject("java","net.sourceforge.pinyin4j.format.HanyuPinyinToneType");
		var HanyuPinyinVCharType = CreateObject("java","net.sourceforge.pinyin4j.format.HanyuPinyinVCharType");
		var PinyinHelper = CreateObject("java","net.sourceforge.pinyin4j.PinyinHelper");
		var BadHanyuPinyinOutputFormatCombination = CreateObject("java","net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination");
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_UNICODE);

		var result='';
		for(var i=1; i<=len(s); i++){
			singleLetter = JavaCast('string',mid(s,i,1));
			//先要判断是否是中文范围
			if( isarray( PinyinHelper.toHanyuPinyinStringArray(singleLetter, format)) ){
				if(mode!=0){
					result = result & left(PinyinHelper.toHanyuPinyinStringArray(singleLetter, format)[1],1);
				}else{
					result = result & PinyinHelper.toHanyuPinyinStringArray(singleLetter, format)[1];
				}
			}else{
				result = result & singleLetter;
			}
		}

		return result;
	}


	function translateToEn(string s){
		var targetUrl = 'http://fanyi.baidu.com/transapi?from=zh&to=en&query=' & s;
		http method="GET" url="#targetUrl#" result="result";
		var tran = DeserializeJSON(result.filecontent);
		return tran.data[1].dst;
	}
</cfscript>
